/*****************************************************************************
 *   Copyright 2005 Tim A Wang
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 ****************************************************************************/
package com.jb.web.context;

import java.io.IOException;

import com.jb.faces.component.UIComponent;
import com.jb.faces.context.ResponseWriter;


public class ResponseWriterImpl extends ResponseWriter {
    String characterEncoding;
    String contentType;
    private java.io.Writer writer;

    public ResponseWriterImpl(java.io.Writer writer, java.lang.String contentTypeList,
        java.lang.String characterEncoding) {
        this.writer = writer;
        this.contentType = contentTypeList;
        this.characterEncoding = characterEncoding;
    }

    public void setCharacterEncoding(String characterEncoding) {
        this.characterEncoding = characterEncoding;
    }

    public String getCharacterEncoding() {
        return characterEncoding;
    }

    public void setContentType(String contentType) {
        this.contentType = contentType;
    }

    public String getContentType() {
        return contentType;
    }

    public ResponseWriter cloneWithWriter(java.io.Writer writer) {
        return new ResponseWriterImpl(writer, getContentType(), getCharacterEncoding());
    }

    public void close() throws IOException {
        writer.close();
    }

    public void endDocument() {
    }

    public void endElement(String tagName) {
        writeText(null, "</" + tagName + ">");
    }

    public void flush() throws IOException {
        writer.flush();
    }

    public void startDocument() {
    }

    public void startElement(String tagName, UIComponent comp) {
        writeText(null, "<" + tagName + ">");
    }

    public void write(char[] c, int from, int to) {
        try {
            writer.write(c, from, to);
        } catch (Exception e) {
        }
    }

    public void writeAttribute(String name, Object o, String text) {
        writeText(o, ((text == null) || (text.length() == 0)) ? "" : (name + "=\"" + text + "\""));
    }

    public void writeComment(Object obj) {
        //
    }

    public void writeText(Object o, String text) {
        try {
            writer.write(text);
        } catch (Exception e) {
            //
        }
    }

    public void writeText(char[] c, int from, int to) throws IOException {
        writer.write(c, from, to);
    }

    public void writeURIAttribute(String name, Object o, String text) {
        writeAttribute(name, o, text);
    }
}
